--[[
FilePath: \Lua\View\UIActivityExplor\UIActivityExplor.lua
游园活动
--]]

require 'View/UIBase/UIBase'

UIActivityExplor = UIBase.New(PanelNames.UIActivityExplor)
local _M = UIActivityExplor

_M.hasInit = false;
_M.hasView = false;
_M.curPosIndex = 1;
_M.serverPosIndex = 1;
_M.progressIndex = 0;
_M.proRewardIndex = 1;
_M.maxPosNum = 39;
_M.moveSp = 500;
_M.forNum = 20;
_M.costNum1 = 1;
_M.costNum10 = 10;
_M.getRewardNum1 = 1;
_M.getRewardNum10 = 10;
_M.hasGetReward = false;
_M.isRewardMove = false;
_M.serverRewardItem = nil;
_M.serverRewardExtraItems = nil;
_M.isProgressMax = false;
_M.rankItem = nil;
_M.subRankTableDataList = nil;
_M.rankServerDataList = nil;
_M.taskDataList = nil;

function _M.Init(func, id)
    _M.activeId = id
    UIBase.Init(_M, func)
end

function _M:OnCreate()
    if not _M.hasInit then
        self:RegisterOnClick(self:GetChild("Btn_Back"), self.OnClose);
        self.title = self:GetChild("Com_Title"):GetChild("title");
        self.btnHelp = self:GetChild("Btn_Help");
        self.cashLb = self:GetChild("CashLb");
        self.btnHero = self:GetChild("HeroButton");
        self.btnHolyStone = self:GetChild("HolyStoneButton");
        self.btnHolyStoneRedCtrl = self.btnHolyStone:GetChild("UIRedDots"):GetController("c1");
        self.btnBless = self:GetChild("BlessButton");
        self.btnRank = self:GetChild("CommonButtonCircular");
        self.btnTask = self:GetChild("CommonButtonTask");
        self.taskRedCtrl = self.btnTask:GetChild("UIRedDots"):GetController("c1");
        self.txtCountDown = self:GetChild("Text_CountDown");
        self.btnTravel = self:GetChild("Btn_Travel");
        self.btnTravel10 = self:GetChild("Btn_Travel10");
        self.barRes = self:GetChild("Bar_Res");
        self.btnAdd =  self.barRes:GetChild("add");
        self.barTravel = self:GetChild("Bar_Travel");
        self.txtTravelProcess = self:GetChild("Text_TravelProcess");
        self.txtRewardPreview = self:GetChild("Text_RewardPreview");
        self.listRewardPreview = self:GetChild("List_RewardPreview");
        self.Graph_Model = self:GetChild("Graph_Model");
        self.mapNode = self:GetChild("Com_Map");
        self.comDots = {};
        self.comDotBoxs = {};
        self.boxTimers = {};
        self.boxHideList = {};
        for i = 1, self.maxPosNum do
            local dot;
            if i < 10 then
                dot = self.mapNode:GetChild("Com_Dot0"..i);
            else
                dot = self.mapNode:GetChild("Com_Dot"..i);
            end
            table.insert(self.comDots, dot);
            table.insert(self.comDotBoxs, dot:GetChild("effect_ChapterBox"));
        end
        self.bannerScoer = self:GetChild("bannerscoer");
        self.rank2tips = self:GetChild("rank2tips");

        self.rankShowCtrl = self:GetController("RankShow");
        local rankNode = self:GetChild("UIWeekRankFight");
        self.rankList = rankNode:GetChild("ranklist");
        self.rankList.itemRenderer = self.setRankItem;
        self.rankList:SetVirtual();
        self.myRank = rankNode:GetChild("my");
        self.btnCloseRank = rankNode:GetChild("btnClose");

        self.taskShowCtrl = self:GetController("TaskShow");
        local taskNode = self:GetChild("UITaskList");
        self.taskList = taskNode:GetChild("TaskList");
        self.taskList.itemRenderer = self.setTaskItem;
        self.taskList:SetVirtual();
        self.btnCloseTask = taskNode:GetChild("btnClose");

        _M.hasInit = true

        self:addEvent()
    end
end

function _M:addEvent()
    self:RegisterEvent(Config.EventType.Fresh_Active,self.Refresh);
    self:RegisterEvent(Config.EventType.Fresh_Activity_Turntable_Rank,self.ShowRank);
    self.btnHelp.onClick:Add(self.HelpTip, self)
    self.btnTravel.onClick:Add(self.clickBtnTravel, self)
    self.btnBless.onClick:Add(self.clickBtnBless, self)
    self.btnHero.onClick:Add(self.clickBtnHero, self)
    self.btnHolyStone.onClick:Add(self.clickBtnHolyStone, self)
    self.btnAdd.onClick:Add(self.clickBtnAdd, self)
    self.listRewardPreview.scrollPane.onScrollEnd:Add(self.rewardListScrollEnd);
    self.listRewardPreview.onTouchBegin:Add(self.rewardListDragBegin);
    self.listRewardPreview.onTouchEnd:Add(self.rewardListDragEnd);
    self.btnRank.onClick:Add(self.clickBtnRank, self);
    self.btnTask.onClick:Add(self.clickBtnTask, self);
    self.btnCloseRank.onClick:Add(self.clickBtnCloseRank, self);
    self.btnCloseTask.onClick:Add(self.clickBtnCloseTask, self);
end

function _M:removeEvent()
    self.btnHelp.onClick:Remove(self.HelpTip)
    self.btnTravel.onClick:Remove(self.clickBtnTravel)
    self.btnBless.onClick:Remove(self.clickBtnBless)
    self.btnHero.onClick:Remove(self.clickBtnHero)
    self.btnHolyStone.onClick:Remove(self.clickBtnHolyStone)
    self.btnAdd.onClick:Remove(self.clickBtnAdd)
    self.listRewardPreview.scrollPane.onScrollEnd:Remove(self.rewardListScrollEnd);
    self.listRewardPreview.onTouchBegin:Clear();
    self.listRewardPreview.onTouchEnd:Clear();
    self.btnRank.onClick:Remove(self.clickBtnRank);
    self.btnTask.onClick:Remove(self.clickBtnTask);
    self.btnCloseRank.onClick:Remove(self.clickBtnCloseRank);
    self.btnCloseTask.onClick:Remove(self.clickBtnCloseTask);
end

function _M.rewardListDragBegin()
    _M.isRewardMove = true;
    _M:removeListMoveTime();
end

function _M.rewardListDragEnd()
    _M:removeListMoveTime();
    _M.listMoveTimer = utimer.timeCall(function()
        _M.isRewardMove = false;
        _M.rewardListScrollEnd();
    end,2000,1);
end

function _M.rewardListScrollEnd()
    if _M.isRewardMove then
        return;
    end
    _M:removeListMoveTime();
    if _M.listRewardPreview.scrollPane.isRightMost then
        _M.listMoveTimer = utimer.timeCall(function()
            _M.listRewardPreview.scrollPane:ScrollLeft(0.1, true);
        end,100,-1);
    else
        _M.listMoveTimer = utimer.timeCall(function()
            _M.listRewardPreview.scrollPane:ScrollRight(0.1, true);
        end,100,-1);
    end
end

function _M:OnClose()
    if self.hasGetReward then
        return;
    end
    self:OnBack();
end

function _M:HelpTip()
    if self.hasGetReward then
        return;
    end
    if self.tableData ~= nil and self.tableData.f_ExplainTitle ~= nil and self.tableData.f_ExplainText ~= nil then
        logicMgr.UIShowManager.ShowCommonTips(tostring(self.tableData.f_ExplainTitle), tostring(self.tableData.f_ExplainText));
    end
end

function _M:clickBtnTravel()
    if self.hasGetReward then
        return;
    end
    local haveNum = logicMgr.ItemManager.getBagNumByTableID(tonumber(self.tableData.f_CostId));
    local name = logicMgr.ItemManager.getItemConstDatabyModelId(self.tableData.f_CostId).name;
    local selectNum = 1;
    if self.btnTravel10.selected then
        self.moveSp = self.tableData.f_MoveSpeedTen;
        selectNum =  self.getRewardNum10;
        if haveNum < self.costNum10 then
            UISysTips.AddMsg(string.format(getLanguage("Character_Tips19"),name));
            return;
        end
    else
        self.moveSp = self.tableData.f_MoveSpeedOne;
        selectNum =  self.getRewardNum1;
        if haveNum < self.costNum1 then
            UISysTips.AddMsg(string.format(getLanguage("Character_Tips19"),name));
            return;
        end
    end
    self.hasGetReward = true;
    ActivityHandler.sendReqExplorAwardMessage(self.activeId, selectNum)
    --TAManager.TrackActivityEvent(self.activeId, EnumConst.ActivityTrack.ExplorStart, selectNum)
end

function _M.onGetReward(items, extraItems)
    local tableData, serverData = logicMgr.ActiveManager.getActiveData(_M.activeId);
    if serverData ~= nil then
        _M.serverPosIndex = serverData.serverPosIndex;
    end
    _M.serverRewardItem = items;
    _M.serverRewardExtraItems = extraItems;

    local serverRankInfo = logicMgr.ActiveManager.GetActivityExplorRankInfo(_M.activeId);
    if serverRankInfo ~= nil then
        _M.bannerScoer.text = string.format(getLanguage("ActivityTips19001015"), serverRankInfo.selfOrderVal);
    end

    _M:moveMap();
end

function _M.onGetRewardFail()
    _M.hasGetReward = false;
end

function _M:clickBtnBless()
    if self.hasGetReward then
        return;
    end
    showUI(PanelNames.UIExplorGuide, self.tableData);
end

function _M:clickBtnHero()
    if self.hasGetReward then
        return;
    end
    showUI(PanelNames.UIExplorShop, nil, self.tableData.f_ShopActId);
end

function _M:clickBtnAdd()
    self:clickBtnHolyStone();
end

function _M:clickBtnHolyStone()
    if self.hasGetReward then
        return;
    end
    showUI(PanelNames.UIExplorGift, nil, self.tableData.f_GiftActId);
    self.btnHolyStoneRedCtrl.selectedIndex = 0;
    if logicMgr.ActiveManager.getActiveOpenStatus(self.activeId, 1) then
        logicMgr.ActiveManager.updateActiveOpenStatus(self.activeId, 1);
    end
end

function _M:clickBtnRank()
    self.rankShowCtrl.selectedIndex = 1;
    self:getServerRankData();
    self:ShowRank();
end

function _M:getServerRankData()
    local serverRankInfo = logicMgr.ActiveManager.GetActivityExplorRankInfo(self.activeId)
    local version = -1
    if serverRankInfo then
        version = serverRankInfo.rankVersion
    end
    ActivityHandler.sendReqExplorRankListInfoMessage(self.activeId,version);
end

function _M:clickBtnCloseRank()
    self.rankShowCtrl.selectedIndex = 0;
end

function _M:clickBtnTask()
    if self.hasGetReward then
        return;
    end
    self.taskShowCtrl.selectedIndex = 1;
    self:ShowTask();
end

function _M:clickBtnCloseTask()
    self.taskShowCtrl.selectedIndex = 0;
end

function _M:ShowRank()
    local serverRankInfo = logicMgr.ActiveManager.GetActivityExplorRankInfo(self.activeId);
    if self.rankShowCtrl.selectedIndex == 1 then
        if self.subRankTableDataList == nil then
            self.subRankTableDataList = {};
            local cfgs = dataTable.getDataTable("t_turntablerankaward")
            for k,v in pairs(cfgs) do
                if v.f_ActivityId == self.activeId then
                    table.insert(self.subRankTableDataList,v)
                end
            end
            table.sort(self.subRankTableDataList, function (a, b)
                return a.f_RankId < b.f_RankId;
            end);
        end
        local myRankServerData = {};
        myRankServerData.playerId = logicMgr.HeroManager.GetRoleId();
        myRankServerData.name = logicMgr.HeroManager.GetRoleName();
        myRankServerData.pic = logicMgr.HeroManager.GetRoleHeadID();
        myRankServerData.picFrame = logicMgr.HeroManager.GetRoleHeadFrame();
        myRankServerData.lv = logicMgr.HeroManager.GetRoleLevel();
        myRankServerData.rank = 0;
        myRankServerData.orderVal = 0
        if serverRankInfo then
            myRankServerData.rank = math.max(serverRankInfo.selfRank,0)
            myRankServerData.orderVal = serverRankInfo.selfOrderVal;
            self.rankServerDataList = serverRankInfo.rankList or {}
        else
            self.rankServerDataList = {};
        end
        local myTableData = nil;
        if myRankServerData.rank > 0 then
            myTableData = self:getTableData(myRankServerData.rank);
        end
        self:updateItem(self.myRank, myRankServerData, myTableData, myRankServerData.rank, myRankServerData.rank, true);
        self.rankList.numItems = 50;
    end
    if serverRankInfo == nil then
        self.rank2tips.text = string.format(getLanguage("ActivityTips19001014"), getLanguage("NotInRank"));
        self.bannerScoer.text = string.format(getLanguage("ActivityTips19001015"), 0);
    else
        if serverRankInfo.selfRank < 1 then
            self.rank2tips.text = string.format(getLanguage("ActivityTips19001014"), getLanguage("NotInRank"));
        else
            self.rank2tips.text = string.format(getLanguage("ActivityTips19001014"), serverRankInfo.selfRank);
        end
        self.bannerScoer.text = string.format(getLanguage("ActivityTips19001015"), serverRankInfo.selfOrderVal);
    end
end

function _M.setRankItem(index, itemObj)
    index = index + 1;
    _M:updateItem(itemObj, _M.rankServerDataList[index], _M:getTableData(index), index, index);
end

function _M:getTableData(rank)
    for k, v in pairs(self.subRankTableDataList) do
        local rankCfg = decodeJsonStrToTabel(v.f_Rank)
        if rank >= rankCfg[1] and rank <= rankCfg[2] then
            return v;
        end
    end
    return nil;
end

function _M:updateItem(itemObj, serverData, tableData, rankIndex, index, isMe)
    local typeController = itemObj:GetController("c1");
    local rankController = itemObj:GetController("rank");
    local ctrlNoRankController = itemObj:GetController("ctrlNoRank");
    if index > 100 or rankIndex == 0 or (isMe and serverData == nil) then
        ctrlNoRankController.selectedIndex = 1;
    else
        ctrlNoRankController.selectedIndex = 0;
    end
    if isMe then
        typeController.selectedIndex = 2;
    else
        typeController.selectedIndex = 0;
    end
    if tableData ~= nil then
        logicMgr.CostManager.RefreshItemList(itemObj:GetChild("itemlist"), tableData.f_Award, 3);
    end
    typeController.selectedIndex = 0;
    local head = itemObj:GetChild("head");
    head.onClick:Clear();
    local rank = rankIndex;
    if serverData ~= nil then
        itemObj:GetChild("name").text = serverData.name;
        itemObj:GetChild("level").text = getLanguage("Sys_HeroLevel")..serverData.lv;
        itemObj:GetChild("fight").text = tostring(serverData.orderVal);
        itemObj:GetChild("lv").text = "级";
        UIComponentInterface:SetPlayerIcon(head:GetChild("head"):GetChild("HeadIcon"), serverData.pic, head:GetChild("frame"), serverData.picFrame);
        if serverData.playerId == logicMgr.HeroManager.GetRoleId() then
            typeController.selectedIndex = 2;
        end
        --rank = serverData.rank or 0;
    else
        itemObj:GetChild("name").text = ""
        itemObj:GetChild("level").text = ""
        itemObj:GetChild("fight").text = ""
        itemObj:GetChild("lv").text = ""
        head:GetChild("head"):GetChild("HeadIcon").url = EnumConst.AtlasPath.CommonUI.."icon_hero_kb91";
        head:GetChild("frame").url = "";
    end
    if rank <= 3 and rank > 0 then
        rankController.selectedIndex = rank - 1;
    else
        rankController.selectedIndex = 3;
        itemObj:GetChild("rank").text = rank;
    end
    if rank <= 0 then
        rankController.selectedIndex = 3;
        itemObj:GetChild("rank").text = getLanguage("NotInRank");
    else
        if isMe and rank > 100 then
            itemObj:GetChild("rank").text = getLanguage("NotInRank");
        else
            itemObj:GetChild("rank").text = rank;
        end
    end
end

function _M:ShowTask()
    self.taskDataList = self.serverData.explorTaskData;
    table.sort(self.taskDataList,function(a,b)
        local aState = self:GetRewardState(a.f_ActTaskId);
        local bState = self:GetRewardState(b.f_ActTaskId);
        if aState ~= bState then
            return aState > bState;
        end
        return a.f_ActTaskId < b.f_ActTaskId;
    end)
    self.taskList.numItems = #self.taskDataList;
end

--0:未完成，1：完成未领，2：完成已领
function _M:GetRewardState(id)
    local data = self.serverData.taskData[tostring(id)];
    if data and data.recvStatus == 1 then
        return 0;
    end
    local cfg = dataTable.getDataTableByStringId("t_periodtask",id)
    if(data and data.process >= cfg.f_Progress) then
        return 2;
    end
    return 1;
end

function _M.setTaskItem(index, itemObj)
    index = index + 1
    local itemList = itemObj:GetChild("itemList")
    local statusCtrl = itemObj:GetController("status");
    local title = itemObj:GetChild("title");
    local btnGet = itemObj:GetChild("btn_Get");
    local cfg = _M.taskDataList[index];
    logicMgr.CostManager.RefreshItemList(itemList, cfg.f_Award);
    local count = 0
    local data = _M.serverData.taskData[tostring(cfg.f_ActTaskId)];
    if data ~= nil then
        count = data.process;
    end
    title.text = string.format(getLanguage(cfg.f_ActTaskDesc),count);
    local state = _M:GetRewardState(cfg.f_ActTaskId);
    if state == 0 then
        state = 2;
    else
        state = state - 1;
    end
    statusCtrl.selectedIndex = state
    btnGet.onClick:Clear();
    if state == 0 then --未完成

    elseif state == 1 then
        --未领取
        btnGet.onClick:Add(function()
            --if this.isClick then
            --    return
            --end
            --this.isClick = true
            ActivityHandler.sendReqRecvExplorTaskAwardMessage(_M.activeId, cfg.f_ActTaskId);
        end)
    end
end

-- 显示
function _M:OnEnable()
    --if self.activeId ~= nil then
       -- TAManager.TrackActivityEvent(self.activeId, EnumConst.ActivityTrack.EnterView)
    --end
    self:Refresh();
end

function _M:Refresh()
    if self.activeId == nil or not _M.hasInit then
        self:OnBack();
        return;
    else
        self.tableData, self.serverData = logicMgr.ActiveManager.getActiveData(self.activeId);
        if self.serverData == nil then
            self:OnBack();
            return;
        end
    end
    self:getServerRankData();
    self.serverPosIndex = self.serverData.serverPosIndex;
    self:initView();
    local haveNum = logicMgr.ItemManager.getBagNumByTableID(tonumber(self.tableData.f_CostId));
    self.cashLb.title = getNumString(haveNum);
    self.barRes.title = getNumString(haveNum);

    self.btnHolyStoneRedCtrl.selectedIndex = 0;
    if logicMgr.ActiveManager.getActiveOpenStatus(self.activeId, 1) then
        self.btnHolyStoneRedCtrl.selectedIndex = 2;
    end

    if self.taskShowCtrl.selectedIndex == 1 then
        self:ShowTask();
    end
    self.taskRedCtrl.selectedIndex = 0;
    if self.serverData.taskRed then
        self.taskRedCtrl.selectedIndex = 2;
    end

    local time = HelperFunc.GetServerTime();
    self.endCountDown = self.serverData.endTime - time;
    self:updateActiveCountDown();
    self:TimeCall(self.updateActiveCountDown, 1000, -1);
end

function _M:updateActiveCountDown()
    if self.endCountDown == nil then
        return;
    end
    local showTimeTips = self.txtCountDown
    if self.showTimeMode == 2 then
        showTimeTips = self.txtChunjieTips
    end
    if self.endCountDown <= 0 then
        showTimeTips.text =  getLanguage("Sys_ActivityEnd");
        self:RemoveTimeCall(self.updateActiveCountDown)
    else
        showTimeTips.text =  string.format(getLanguage("Sys_ActivityTimeLeft"), HelperFunc.GetSurpTimeStr(self.endCountDown));
    end
    self.endCountDown = self.endCountDown - 1;
end

function _M:initView()
    if self.hasView then
        return;
    end
    self.hasView = true;
    self.btnHero.text = getLanguage("ActivityExplorTips02");
    self.btnHolyStone.text = getLanguage("ActivityExplorTips03");
    self.btnBless.text = getLanguage("ActivityExplorTips04");

    self.curPosIndex = self.serverPosIndex;
    self.progressIndex = self.serverPosIndex - 1;
    local icon =  EnumConst.AtlasPath.ItemAtlas .. G.dataTable["t_item"][tostring(self.tableData.f_CostId)].f_Icon;
    self.cashLb.icon = icon;
    self.barRes.icon = icon;

    local itemCfg = dataTable.getDataTableByStringId(self.tableData.f_ItemTableName, self.activeId)
    if itemCfg == nil then
        self:OnBack();
        return;
    end
    self.proRewardCfg = decodeJsonStrToTabel(itemCfg.f_Raward);
    self.progressMax = tonumber(itemCfg.f_Times);
    self:showProgress();
    local costCfg = decodeJsonStrToTabel(itemCfg.f_DrawConsume);
    self.costNum1 = costCfg[1][2];
    self.getRewardNum1 = costCfg[1][3];
    self.costNum10 = costCfg[2][2];
    self.getRewardNum10 = costCfg[2][3];

    local heroID = tostring(logicMgr.SkinManager.getHeroModelIdBySkinId(logicMgr.HeroManager.GetRoleInfo("pic")));
    local useSkinId = logicMgr.SkinManager.getHeroSkinId(heroID)
    local imageName = logicMgr.HeroManager.GetHeroDrawingRolePath(heroID,nil,useSkinId);
    local posIndex = self:getPosIndex(self.serverPosIndex);
    local tagX = self.mapNode.x - (self.comDots[posIndex].x - self.comDots[1].x);
    local tagY = self.mapNode.y - (self.comDots[posIndex].y - self.comDots[1].y);
    self.mapNode:SetXY(tagX, tagY);
    self.forNum = dataTable.getDataTableByStringId("t_global", 665, "f_int_value") or 20;
    local localForNum = self.forNum;
    if self.serverPosIndex < self.maxPosNum then
        localForNum = math.min(self.serverPosIndex, self.forNum)
    end
    localForNum = localForNum - 1;
    for i = 0, localForNum do
        local index = posIndex - i;
        if index <= 0 then
            index = index + self.maxPosNum;
        end
        self.comDotBoxs[index].visible = false;
        self.boxHideList[index] = 1;
    end

    resMgr:LoadModelPrefab(imageName, function(go)
        if (self.role_liHui ~= nil) then
            destroyImmediate(self.role_liHui);
            self.role_liHui = nil;
        end
        if self.drawWrapper == nil then
            self.drawWrapper = GoWrapper.New();
        end

        self.role_liHui = newObject(go);
        self.role_liHui.name = imageName;

        self.role_liHui:AddComponent(TCanvas);

        self.drawWrapper:SetWrapTarget(self.role_liHui, true);
        local heroSize = self.tableData.f_HeroSize;
        local rotationY = 0;
        local prePosX = 0;
        if posIndex == 1 then
            prePosX = self.comDots[self.maxPosNum].x;
        else
            prePosX = self.comDots[posIndex - 1].x;
        end
        rotationY = self.comDots[posIndex].x <= prePosX and 180 or 0;
        Util.Lua_SetLocalRotationXYZ(self.role_liHui, 0, rotationY, 0);
        Util.Lua_SetLocalScaleXYZ(self.role_liHui.transform, heroSize, heroSize, heroSize);
        self.Graph_Model:SetNativeObject(self.drawWrapper);
        self.animObj = Util.Lua_FindGameObject(self.role_liHui, "Animation");
        Util.PlayAction(self.animObj, "fidle");
    end)
    local rewardCfg = decodeJsonStrToTabel(itemCfg.f_RandPool);
    local rewards = {};
    for k,v in pairs(rewardCfg) do
        table.insert(rewards, {v[1]});
    end
    logicMgr.CostManager.RefreshItemList(self.listRewardPreview, rewards);
    _M.listMoveTimer = utimer.timeCall(function()
        _M.listRewardPreview.scrollPane:ScrollRight(0.1, true);
    end,100,-1);
end

function _M:moveMap()
    if self.curPosIndex < self.serverPosIndex then
        -- 移动地图
        local tempVec = Vector3.New(0, 0, 0);
        local tempPos = Vector3.New(0, 0, 0);
        local mapX = self.mapNode.x;
        local mapY = self.mapNode.y;
        tempPos:Set(mapX, mapY);
        local posIndex = self:getPosIndex(self.curPosIndex);
        local nextPosX;
        local nextPosY;
        if posIndex == 39 then
            nextPosX = self.comDots[1].x;
            nextPosY = self.comDots[1].y;
        else
            nextPosX = self.comDots[posIndex + 1].x;
            nextPosY = self.comDots[posIndex + 1].y;
        end
        local curPosX = self.comDots[posIndex].x;
        local tagX = mapX - (nextPosX - curPosX);
        local tagY = mapY - (nextPosY - self.comDots[posIndex].y);
        tempVec:Set(tagX, tagY);

        tempPos:Set(tempPos.x - tempVec.x, tempPos.y - tempVec.y);
        local len = tempPos:Magnitude();
        local time = len / self.moveSp;
        if self.mapTween then
            self.mapTween:Kill();
            self.mapTween = nil;
        end
        if self.role_liHui ~= nil then
            local rotationY = nextPosX <= curPosX and 180 or 0;
            Util.Lua_SetLocalRotationXYZ(self.role_liHui, 0, rotationY, 0);
        end
        Util.PlayAction(self.animObj, "move");
        self.mapTween = self.mapNode:TweenMove(tempVec, time):SetEase(EaseType.Linear):OnComplete(function()
            self.curPosIndex = self.curPosIndex + 1;
            posIndex = posIndex == 39 and 1 or (posIndex + 1);
            Util.PlayAction(self.comDotBoxs[posIndex].wrapTarget, "idle2", false);
            self:refreshBoxState();
            self.boxTimers[posIndex] = utimer.timeCall(function()
                self.comDotBoxs[posIndex].visible = false;
                self.boxHideList[posIndex] = 1;
            end,850,1);
            -- 检测可以显示的宝箱
            self:moveMap();
        end);
    else
        -- 弹奖励
        Util.PlayAction(self.animObj, "fidle");
        self:removeRewardTime();
        self.showRewardTimers = utimer.timeCall(function()
            self:removeBoxTime();
            self:showReward(_M.serverRewardItem, function()
                -- 跑进度条
                self.proMoveTime = 0.2;
                if (self.progressIndex + 1) < (self.serverPosIndex - 1)then
                    self.proMoveTime = 0.1;
                end
                self:moveProgress();
            end)
        end,900,1);
    end
end

function _M:moveProgress()
    --self:removeRewardTime();
    --self.showRewardTimers = utimer.timeCall(function()
    --    if self.progressIndex >= (self.serverPosIndex - 1) or self.isProgressMax then
    --        self:removeRewardTime();
    --        self:showReward(_M.serverRewardExtraItems);
    --        _M.hasGetReward = false;
    --    else
    --        self.progressIndex = self.progressIndex + 1;
    --        self:showProgress();
    --    end
    --end,100,-1);


    if self.progressIndex >= (self.serverPosIndex - 1) or self.isProgressMax then
        self:removeRewardTime();
        self:showReward(_M.serverRewardExtraItems);
        self.hasGetReward = false;
    else
        self.progressIndex = self.progressIndex + 1;
        local curProNum = self.progressIndex % self.progressMax;
        if curProNum == 0 then
            curProNum = self.progressMax;
        end
        local t1 = self.barTravel:TweenValue(curProNum, self.proMoveTime)
        t1:OnComplete(function()
            if curProNum == self.progressMax then
                self.barTravel.value = 0;
                curProNum = 0;
            end
            self.txtTravelProcess.text = curProNum.."/"..self.progressMax;
            t1:Kill();
            self:moveProgress();
        end)
    end
end

function _M:showProgress()
    --if self.isProgressMax then
    --    return;
    --end
    --local cfg = self.proRewardCfg[self.proRewardIndex];
    --local cfgNum = #self.proRewardCfg;
    --if self.progressIndex >= cfg[1] and self.proRewardIndex < cfgNum then
    --    self.proRewardIndex = self.proRewardIndex + 1;
    --    cfg = self.proRewardCfg[self.proRewardIndex];
    --end
    --local preProNum = 0;
    --if self.proRewardIndex > 1 then
    --    preProNum = self.proRewardCfg[self.proRewardIndex - 1][1];
    --end
    --local maxNum = cfg[1] - preProNum;
    --if self.proRewardIndex == #self.proRewardCfg and self.progressIndex >= cfg[1] then
    --    self.barTravel.value = 10;
    --    self.txtTravelProcess.text = maxNum.."/"..maxNum;
    --    self.isProgressMax = true;
    --else
        local curProNum = self.progressIndex % self.progressMax;
        self.barTravel.value = math.min(curProNum / self.progressMax * 10, self.progressMax);
        self.txtTravelProcess.text = curProNum.."/"..self.progressMax;
    --end
end

function _M:showReward(data, func)
    if data ~= nil then
        local rewards = {};
        for k,v in pairs(data) do
            local itemData = logicMgr.CostManager.getItemOrEquipDataByIdAndNum(v.itemModelId, v.num);
            if itemData ~= nil then
                table.insert(rewards, itemData);
            end
        end
        if #rewards > 0 then
            ShowSpItemGetPanel(rewards, function()
                if func ~= nil then
                    func();
                end
            end, false);
        else
            if func ~= nil then
                func();
            end
        end
    end
end

function _M:refreshBoxState()
    local posIndex = self:getPosIndex(self.curPosIndex);
    for k, v in pairs(self.boxHideList) do
        if v == 1 and posIndex ~= k then
            if k < posIndex then
                if (k + self.forNum) < posIndex then
                    self.comDotBoxs[k].visible = true;
                    Util.PlayAction(self.comDotBoxs[k].wrapTarget, "idle", true);
                    self.boxHideList[k] = 0;
                end
            else
                if (posIndex + self.forNum) > k then
                    self.comDotBoxs[k].visible = true;
                    Util.PlayAction(self.comDotBoxs[k].wrapTarget, "idle", true);
                    self.boxHideList[k] = 0;
                end
            end
        end
    end
end

function _M:getPosIndex(curPosIndex)
    local posIndex = curPosIndex % self.maxPosNum;
    posIndex = math.ceil(posIndex);
    posIndex = posIndex == 0 and self.maxPosNum or posIndex;
    return posIndex;
end

-- 隐藏
function _M:OnDisable()

end

function _M:removeListMoveTime()
    if self.listMoveTimer ~= nil then
        utimer.remove(self.listMoveTimer);
        self.listMoveTimer = nil;
    end
end

function _M:removeRewardTime()
    if self.showRewardTimers ~= nil then
        utimer.remove(self.showRewardTimers);
        self.showRewardTimers = nil;
    end
end

function _M:removeBoxTime()
    for k, v in pairs(self.boxTimers) do
        if v ~= nil then
            utimer.remove(v);
        end
    end
    self.boxTimers = {};
end

-- 卸载
function _M:OnDestroy()
    self:removeEvent();
    if self.role_liHui ~= nil then
        destroy(self.role_liHui);
        self.role_liHui = nil;
    end
    if self.drawWrapper then
        self.drawWrapper:Dispose();
        self.drawWrapper = nil;
    end
    if self.rankItem then
        self.rankItem.OnDestroy();
    end
    self:removeListMoveTime();
    self:removeRewardTime();
    self:removeBoxTime();
    self.hasInit = false;
    self.hasView = false;
    self.curPosIndex = 1;
    self.serverPosIndex = 1;
    self.progressIndex = 0;
    self.proRewardIndex = 1;
    self.moveSp = 500;
    self.forNum = 20;
    self.costNum1 = 1;
    self.costNum10 = 10;
    self.getRewardNum1 = 1;
    self.getRewardNum10 = 10;
    self.hasGetReward = false;
    self.isRewardMove = false;
    self.serverRewardItem = nil;
    self.serverRewardExtraItems = nil;
    self.proRewardCfg = nil;
    self.isProgressMax = false;
    self.rankItem = nil;
    self.subRankTableDataList = nil;
    self.rankServerDataList = nil;
    self.taskDataList = nil;
end

return _M;